package vip.xiaonuo.util;

import vip.xiaonuo.modular.manage.biindextype.entity.BiIndexType;
import vip.xiaonuo.modular.entitys.Tree;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 主题指标分类管理辅助类
 * @Author cy
 * @Date 2022/8/1 17:34
 * @Version 1.0
 */
public class ThemeIndexTypeHandler extends BIndexTypeHandler<Tree> {

    @Override
    public  List<Tree> allTree(List<BiIndexType> indexTypes) {
        Tree rootNode = getRootNode();
        return getChildren(rootNode,indexTypes);
    }

    @Override
    public Tree rootTree(List<BiIndexType> indexTypes) {
        Tree rootNode = getRootNode();
        rootNode.setChildren(getChildren(rootNode,indexTypes));
        return rootNode;
    }

    @Override
    public Tree getRootNode(){
        Tree rootNode = new Tree();
        rootNode.setTitle("指标分类管理");
        rootNode.setKey(0L);
        return rootNode;
    }

    @Override
    public  List<Tree> getChildren(Tree tree, List<BiIndexType> indexTypes){
        List<Tree> result = new ArrayList<>();
        for(BiIndexType indexType:indexTypes){
            if(isChild(tree,indexType)){
                Tree tree1 = new Tree();
                tree1.setTitle(indexType.getName());
                tree1.setKey(indexType.getId());
                tree1.setChildren(getChildren(tree1,indexTypes));
                result.add(tree1);
            }
        }
        return result;
    }

    @Override
    public  boolean isChild(Tree tree,BiIndexType indexType){
        return tree.getKey().compareTo(indexType.getPid())==0;
    }
}
